package optimizers.simplega.report;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import optimizers.ganeat.Constants;
import optimizers.simplega.Individual;

public class LoggerSGA {
	private String filename;
	private boolean showInConsole;

	public StringBuilder builder;
	double[][] fitnessData = new double[4][Constants.generations + 1];
	double[][] templateData = new double[4][Constants.generations + 1];
	double[][] SDlogKData = new double[4][Constants.generations + 1];
	ArrayList<Individual> bestIndividuals;

	public LoggerSGA(String filename, boolean showInConsole,
			double[][] fitnessData, double[][] templateData,
			double[][] SDlogKData, ArrayList<Individual> bestIndividuals) {
		System.out.println(filename);
		this.filename = filename;
		this.showInConsole = showInConsole;
		this.fitnessData = fitnessData;
		this.templateData = templateData;
		this.SDlogKData = SDlogKData;
		this.bestIndividuals = bestIndividuals;
		this.builder = new StringBuilder();
	}

	public void logToMemory(String message) {
		builder.append(message + "\r\n");
		if (showInConsole) {
			System.out.println(message);
		}
	}

	public void saveToFile() {
		try {
			FileOutputStream saveFile = new FileOutputStream(filename);
			ObjectOutputStream save = new ObjectOutputStream(saveFile);
			save.writeObject(fitnessData);
			save.writeObject(templateData);
			save.writeObject(SDlogKData);
			save.writeObject(builder.toString());
			save.writeObject(bestIndividuals);
			save.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
